package com.hanson.common.util;


//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
//import org.springframework.core.io.ClassPathResource;
//
//import javax.imageio.ImageIO;
//import java.awt.*;
//import java.awt.font.FontRenderContext;
//import java.awt.font.TextAttribute;
//import java.awt.geom.RoundRectangle2D;
//import java.awt.image.BufferedImage;
//import java.io.BufferedOutputStream;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.URL;
//import java.text.AttributedString;
//import java.util.ArrayList;
//import java.util.List;
//
//public class ImageUtil2 {
//    private static int imageWidth = 1080;// 图片的宽度
//
//    private static int imageHeight = 1920;// 图片的高度
//
//    public static void main(String[] args) throws IOException {
//        FileOutputStream fos = new FileOutputStream("D:\\log\\1.jpg");
//        BufferedOutputStream bos = new BufferedOutputStream(fos);
//        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
//        encoder.encode(createImg("廖廖廖", "1", new String[]{"1", "2", "3", "4", "5"}));
//        bos.close();
//        fos.close();
//    }
//
//
//    public static BufferedImage createImg(String name, String title, String[] tagList) throws IOException {
//        BufferedImage image = new BufferedImage(imageWidth, imageHeight,
//                BufferedImage.TYPE_INT_RGB);
//
//        Graphics2D g = image.createGraphics();
//        g.fillRect(0, 0, imageWidth, imageHeight);
//        ClassPathResource resource = new ClassPathResource("bg3.jpg");
//        BufferedImage bgImage = ImageIO.read(resource.getInputStream());
//        g.drawImage(bgImage, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null);
//        g = (Graphics2D) image.getGraphics();
//
//        URL url2 = new URL("http://thirdwx.qlogo.cn/mmopen/vi_32/PiajxSqBRaEIcx9NDueibApk4tISpJ82QCN3hXuIiaL75YYC8BsshQTYAic3jTb53KsgXQC29lo0LRY7dI2qz24QNA/132");
//        InputStream in2 = url2.openConnection().getInputStream();
//        BufferedImage image2 = ImageIO.read(in2);
//        image2 = roundImage(image2, image2.getWidth(), image2.getWidth());
//        g.drawImage(image2, 470, 150, 150, 150, null);
//        g = (Graphics2D) image.getGraphics();
//
//        // 计算文字长度，计算居中的x点坐标
//        g.setColor(Color.BLACK);
//        Font font1 = new Font("黑体", Font.BOLD, 32);
//        g.setFont(font1);
//        g.drawString("优选商品", getCenterX(g, font1, "优选商品"), 350);
//
//        g = (Graphics2D) image.getGraphics();
//        Font font2 = new Font("黑体", Font.BOLD, 32);
//        g.setColor(Color.BLACK);
//        g.setFont(font2);
//        g.drawString("优选商品，为你推荐",  getCenterX(g, font2, "优选商品，为你推荐"), 450);
//
//        URL url4 = new URL("http://kaobaobaotest.oss-cn-qingdao.aliyuncs.com/11ee19ce65bd5797c73462b7e9b6fc6e.jpg?Expires=1908270712&OSSAccessKeyId=LTAIbgEq2YbBUscL&Signature=wP4lvh6QYJay9hR%2BCQDwT9aDdUM%3D");
//        InputStream in4 = url4.openConnection().getInputStream();
//        BufferedImage image4 = ImageIO.read(in4);
//        g.drawImage(image4, 360, 700, 400, 400, null);
//
//        g = (Graphics2D) image.getGraphics();
//        Font font3 = new Font("黑体", Font.BOLD, 32);
//        g.setColor(Color.BLACK);
//        g.setFont(font3);
//        String info = "维生素钙片 只为中老年人打造 保证你的身体每日健康保证你的身体每日健康保证你的身体每日健康";
//        FontRenderContext frc = g.getFontRenderContext();
//        g.getFontRenderContext();
//        java.awt.geom.Rectangle2D stringBounds = font3.getStringBounds(info, frc);
//        double fontWidth = stringBounds.getWidth();
//        List<String> line_list = new ArrayList<>();
//        if (fontWidth <= imageWidth-200) {
//            line_list.add(info);
//        } else {
//            int text_width = imageWidth-200;//输出文本宽度,这里就以画布宽度作为文本宽度测试
//            double bs = fontWidth / text_width;//文本长度是文本框长度的倍数
//            int line_char_count = (int) Math.ceil(info.length() / bs);//每行大概字数
//            int begin_index = 0;
//            while (begin_index < info.length()) {
//                int end_index = begin_index + line_char_count;
//                if (end_index >= info.length()) {
//                    end_index = info.length();
//                }
//                String line_str = info.substring(begin_index, end_index);
//                java.awt.geom.Rectangle2D tempStringBounds = font3.getStringBounds(line_str, frc);
//                int tzcs = 0;//调整次数
//                int tzzs = 1;//调整字数,临时文本的字符串长度大于要求的文本宽度时,每次减少临时文本的字数,然后重新测量文本宽度
//                while (tempStringBounds.getWidth() > text_width) {
//                    line_str = line_str.substring(0, line_str.length() - tzzs);//每次向前 tzzs 个字符重新计算(待优化)
//                    tempStringBounds = font3.getStringBounds(line_str, frc);
//                    tzcs++;
//                }
//                line_list.add(line_str);
//                begin_index = begin_index + line_str.length();
//            }
//        }
//        int y = 1180;
//        for (String line : line_list) {
//            g.drawString(line, getCenterX(g,font3,line), y);
//            y += 50;
//        }
//
//        g = (Graphics2D) image.getGraphics();
//        URL url5 = new URL("http://kaobaobaotest.oss-cn-qingdao.aliyuncs.com/QR675e8951-97df-4070-9e09-a1bd0d1310e3.png?Expires=1597128158&OSSAccessKeyId=LTAIbgEq2YbBUscL&Signature=n0W1bgdL61aqOUgWn1pgiqtEiic%3D");
//        InputStream in5 = url5.openConnection().getInputStream();
//        BufferedImage image5 = ImageIO.read(in5);
//        g.drawImage(image5, 130, 1460, 300, 300, null);
//        return image;
//    }
//
//    private static BufferedImage roundImage(BufferedImage image, int targetSize, int cornerRadius) {
//        BufferedImage outputImage = new BufferedImage(targetSize, targetSize, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g2 = outputImage.createGraphics();
//        g2.setComposite(AlphaComposite.Src);
//        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        g2.setColor(Color.WHITE);
//        g2.fill(new RoundRectangle2D.Float(0, 0, targetSize, targetSize, cornerRadius, cornerRadius));
//        g2.setComposite(AlphaComposite.SrcAtop);
//        g2.drawImage(image, 0, 0, null);
//        g2.dispose();
//        return outputImage;
//    }
//
//    public static int getCenterX(Graphics2D g, Font font, String text) {
//        FontMetrics fm = g.getFontMetrics(font);
//        int textWidth = fm.stringWidth(text);
//        return (imageWidth - textWidth) / 2;
//    }
//}
